package com.example.demo.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.area.dao.Area;
import com.example.demo.area.dao.AreaUser;
import com.example.demo.area.mapper.AreaMapper;
import com.example.demo.area.mapper.AreaUserMapper;
import com.example.demo.area.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname AreaServiceImpl
 * @description
 * @date 2024/01/13 13:27
 * <p>
 * ******************************************************
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService, CommandLineRunner {

    public static final ConcurrentHashMap<Integer, Integer> dataSum = new ConcurrentHashMap<>();

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private AreaUserMapper areaUserMapper;

    @Override
    public void insertAreaBatch(List<Area> areaList) {


        List<Area> list = new ArrayList<>();
        list.add(new Area(1, 1, 0, "江西省"));
        list.add(new Area(11, 2, 1, "九江市"));
        list.add(new Area(111, 3, 11, "都昌县"));
        list.add(new Area(112, 3, 11, "浔阳区"));
        list.add(new Area(113, 3, 11, "柴桑区"));
        list.add(new Area(12, 2, 1, "南昌市"));
        list.add(new Area(121, 3, 12, "南昌县"));
        list.add(new Area(122, 3, 12, "西湖区"));
        list.add(new Area(123, 3, 12, "青山湖区"));
        list.add(new Area(13, 2, 1, "宜春市"));
        list.add(new Area(131, 3, 13, "宜春区1"));
        list.add(new Area(132, 3, 13, "宜春区2"));
        list.add(new Area(133, 3, 13, "宜春区3"));
        saveBatch(list);
    }


    @Override
    public List<Integer> getAreaIds(List<Integer> parentIds) {
        List<Integer> res = new ArrayList<>();
        List<Area> all = areaMapper.selectList(new QueryWrapper<>());
        circle(res, parentIds, all);
        return res;
    }

    private void circle(List<Integer> res, List<Integer> parentIds, List<Area> all) {
        if (CollectionUtils.isEmpty(parentIds)) return;
        List<Integer> subList = all.parallelStream()
                .filter(item -> parentIds.contains(item.getParentId()))
                .map(Area::getId).collect(Collectors.toList());
        res.addAll(subList);
        circle(res, subList, all);
    }

    public void compute() {
        dataSum.clear();
        List<Area> allAreas = areaMapper.getAll();
        Map<Integer, Area> idToArea = new HashMap<>();
        for (Area area : allAreas) {
            idToArea.put(area.getId(), area);
            dataSum.put(area.getId(), 0);
        }
        List<AreaUser> allUsers = areaUserMapper.getAll();
        dataSum.put(0, allUsers.size());
        for (AreaUser user : allUsers) {
            initDataSum1(user, idToArea, dataSum);
        }
    }

    private void initDataSum1(AreaUser user, Map<Integer, Area> idToArea, ConcurrentHashMap<Integer, Integer> dataSum) {
        Integer areaId = user.getAreaId();
        Area area = idToArea.get(areaId);
        while (area != null) {
            dataSum.put(areaId, dataSum.get(areaId) + 1);
            areaId = area.getParentId();
            area = idToArea.get(areaId);
        }
    }

    private void initDataSum(AreaUser user, List<Area> allAreas, ConcurrentHashMap<Integer, Integer> dataSum) {
        Integer areaId = user.getAreaId();
        // 当前最小地区+1
//        dataSum.put(areaId, dataSum.get(areaId) +1);
        // 找到当前地区 一定存在
        Area area = areaMapper.selectById(areaId);
        while (area != null) {
            dataSum.put(areaId, dataSum.get(areaId) + 1);
            areaId = area.getParentId();
//            area = allAreas.stream().filter(item -> item.getId().equals(areaId)).findFirst().get();
            area = areaMapper.selectById((long) areaId);
        }
    }

    @Override
    public void run(String... args) throws Exception {
        Long start = System.currentTimeMillis();
        // TODO 关闭
//        compute();
        Long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));
        dataSum.forEach((key, value) -> {
            System.out.println(key + "--->>>" + value);
        });
    }


}
